# Create your views here.
from django_redis import get_redis_connection
from django.core.paginator import Paginator
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet


from bigpeng2020.serializers import SpitSerializer
from six_it.models import Spit


class SpitViewSet(ModelViewSet):
    serializer_class = SpitSerializer
    queryset = Spit.objects.all().order_by("-publishtime")

    #禁用drf自带的认证机制
    def perform_authentication(self, request):
        pass

    #获取所有一级吐槽 spit/
    def list(self, request):

        spitlist = self.get_queryset().filter(parent=None)

        SpitList = []

        try:
            user = self.request.user
        except Exception:
            user = None

        if user is not None and user.is_authenticated:
            redis_conn = get_redis_connection('spit')
            for spit in spitlist:
                collet_flag = redis_conn.hget("spit_collect_%s"%user.id,str(spit.id))
                thumbup_flag = redis_conn.hget("spit_thumbup_%s"%user.id,str(spit.id))
                if collet_flag:
                    spit.collected =True
                if thumbup_flag:
                    spit.thumbup = True
                SpitList.append(spit)
        else:
            SpitList = spitlist

        res = self.get_serializer(instance=SpitList,many=True)

        return Response(res.data)


    #点赞和取消点赞
    # @action(methods=["PUT"], detail=True, url_path="updatethumbup")
    # def update_thumbup(self,request,id):
    #     try:
    #         user = self.request.user
    #     except Exception:
    #         user = None
    #     spit = self.get_object()
    #
    #
    #     if user is not None and user.is_authenticated:
    #         redis_conn = get_redis_connection('spit')
    #         thumbup_flag = redis_conn.hget("spit_thumbup_%s"%user.id,id)
    #
    #         if thumbup_flag:
    #             spit.thumbup -= 1
    #             spit.save()
    #             redis_conn.hdel('spit_thumbup_%s'%user.id,id)
    #             return Response({'success':True,'message':'取消点赞成功'})
    #
    #         else:
    #             redis_conn.hset("spit_thumbup_%s"%user.id,id,1)
    #             spit.thumbup += 1
    #             spit.save()
    #             return Response({'success':True,'message':'点赞成功'})
    #     else:
    #         return Response({'success':False,'message':'未登录'},status=400)
    #
    #
    # #收藏和取消收藏
    # @action(methods=["PUT"], detail=True, url_path="collect")
    # def collect(self,request,id):
    #     try:
    #         user = self.request.user
    #     except Exception:
    #         user = None
    #
    #     if user is not None and user.is_authenticated:
    #
    #         redis_conn = get_redis_connection('spit')
    #         collect_flag = redis_conn.hget('spit_collect_%s'%user.id,id)
    #
    #         if collect_flag:
    #             redis_conn.hdel('spit_collect_%s'%user.id,id)
    #             return Response({'success':True,'message':'取消收藏成功'})
    #         else:
    #             redis_conn.hset("spit_collect_%s"%user.id,id,1)
    #             return Response({'success':True,'message':'收藏成功'})
    #     else:
    #         return Response({'success':False,'message':'未登录'},status=400)
    #





